#include "cccc_mod_module_tree.h"

#include <iostream>

ModuleTreeNode::ModuleTreeNode() {

}

ModuleTreeNode::ModuleTreeNode(string name) {
	this->children = vector<AbstractTreeNode *> ();
	this->treeType = "ModuleTreeNode";
	this->name = name;
}

ModuleTreeNode::~ModuleTreeNode() {

}

void ModuleTreeNode::addFile(SingleFile * file) {
	this->files.push_back(file);

}

vector<SingleFile *> ModuleTreeNode::getFiles() {
	return files;
}

void ModuleTreeNode::setModule(ModularAtomic * module) {
	this->module = module;
}

ModularAtomic * ModuleTreeNode::getModule() {
	return this->module;
}

SingleFile * ModuleTreeNode::getFileAt(int idx) {
	return files[idx];
}

string getLastName(const string path) {
	int len = path.length();
	int i;
	const char* str;
	str = path.c_str();
	for (i = len; i >= 0; i--) {
		if (str[i] == '/')
			break;
	}
	return path.substr(i + 1, len);
}

SingleFile * ModuleTreeNode::getFileByName(string name) {
	int i;
	for (i = 0; i < this->files.size(); i++) {
//		cout << getLastName(files[i]->getFilename()) << endl;
		if (name == getLastName(files[i]->getFilename())) {
			return files[i];
		}
	}
	return NULL;
}

ModuleTreeNode * ModuleTreeNode::getChildModuleByName(string name) {
	int i;
	for (i = 0; i < this->children.size(); i++) {
		if (name == getLastName(
				((ModuleTreeNode *) children[i])->getModule()->getModuleDef())) {
			return (ModuleTreeNode *) children[i];
		}
	}
	return NULL;
}
